package bmcsoft.test.collection;
import java.io.*;
import java.util.*;
public class TreeSet22_9 {
    public static void main(String[] args) throws Exception {
        //Create a File instance
        String pathname = "C:\\Users\\Alan\\Desktop\\A Lifelong Career.txt";
        File filename = new File(pathname);
        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename));
        BufferedReader br = new BufferedReader(reader);
        String line = br.readLine();

        //Create a TreeMap to hold words as key and count as  value
        TreeMap<String,Integer> map = new TreeMap<String, Integer>();

        String[] words = line.split("[ \n\t\r.,;:!?(){]");
        for (int i = 0;i < words.length;i++){
            String key = words[i].toLowerCase();
            if (key.length() > 0){
                if (map.get(key) == null){
                    map.put(key,1);
                }
                else {
                    int value = map.get(key).intValue();
                    value++;
                    map.put(key,value);
                }
            }
        }
        //Get all entries into a set
        Set<Map.Entry<String ,Integer>> entrySet = map.entrySet();

        //Get key and value from each entry
        for (Map.Entry<String ,Integer>entry: entrySet)
            System.out.println( entry.getKey() + "\t" + entry.getValue());
        reader.close();

    }
}
